package com.njtoyo.taxi.admin.service.business.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.netrides.entity.CompanyBank;
import com.njtoyo.taxi.admin.library.payment.cmb.CmbPayClient;
import com.njtoyo.taxi.admin.mapper.master.entity.CompanyWithdraw;
import com.njtoyo.taxi.admin.mapper.master.enums.CompanyWithdrawState;
import com.njtoyo.taxi.admin.mapper.master.enums.ExamineState;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyBankMapper;
import com.njtoyo.taxi.admin.mapper.master.mapper.CompanyWithdrawMapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.company.AddCompanyWithdrawWrapper;
import com.njtoyo.taxi.admin.service.business.CompanyWithdrawService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Slf4j
@Service
@RequiredArgsConstructor
public class CompanyWithdrawServiceImpl extends ServiceImpl<CompanyWithdrawMapper, CompanyWithdraw> implements CompanyWithdrawService {

    @Autowired
    private CompanyBankMapper companyBankMapper;

    @Override
    public RestResult addWithdraw(AdminUser adminUser, AddCompanyWithdrawWrapper add) {
        CompanyBank companyBank = companyBankMapper.selectOne(Wrappers.<CompanyBank>lambdaQuery().eq(CompanyBank::getBankName, add.getBankName())
                .eq(CompanyBank::getCardNumber, add.getCardNumber())
                .eq(CompanyBank::getNameOnCard, add.getNameOnCard()));
        if (ObjectUtil.isNull(companyBank)) {
            return RestResult.failed("关联的收款信息不存在!");
        }

        String reqNo = IdUtil.getSnowflake(1, 1).nextIdStr();

        CompanyWithdraw withdraw = new CompanyWithdraw();
        withdraw.setCompanyId(add.getCompanyId());
        withdraw.setWithdrawTime(DateUtil.date());
        withdraw.setCreatedAt(DateUtil.date());
        withdraw.setState(CompanyWithdrawState.init);
        withdraw.setAmount(add.getAmount());
        withdraw.setSerialNo(reqNo);
        withdraw.setBankId(companyBank.getId());

        withdraw.setCreateId(adminUser.getId());
        withdraw.setExamineState(ExamineState.init);

        // 提现
        // Map<String, Object> requestResult = CmbPayClient.companyWithdraw(add.getAmount().toString(), reqNo);
        // if ("10000".equals(requestResult.get("code"))) {
        //     withdraw.setState(CompanyWithdrawState.done);
        //     withdraw.setReceiptTime(DateUtil.date().toString());
        // }
        // withdraw.setExtParam(JSON.toJSONString(requestResult));
        baseMapper.insert(withdraw);
        return RestResult.success();
    }

    // 通过审核
    @Override
    @Transactional
    public RestResult pass(AdminUser adminUser, AddCompanyWithdrawWrapper add) {
        // 获取数据
        Long id = add.getId();
        CompanyWithdraw companyWithdraw = baseMapper.selectCompanyWithdrawById(id);
        if(companyWithdraw == null)
            throw new RuntimeException("此数据不存在");


        // 更新数据
        CompanyWithdraw update = new CompanyWithdraw();
        update.setExamineState(ExamineState.pass);
        update.setExamineId(adminUser.getId());
        LambdaUpdateWrapper<CompanyWithdraw> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(CompanyWithdraw::getExamineState,ExamineState.init.getCode())
                .eq(CompanyWithdraw::getId,id);
        if(baseMapper.update(update, lambdaUpdateWrapper)<=0){
            throw new RuntimeException("通过审核失败");
        }

        // 提交申请
        String reqNo = companyWithdraw.getSerialNo();
        Map<String, Object> requestResult = CmbPayClient.companyWithdraw(companyWithdraw.getAmount().toString(), reqNo);
        CompanyWithdraw withdraw = new CompanyWithdraw();
        if ("10000".equals(requestResult.get("code"))) {
            withdraw.setState(CompanyWithdrawState.done);
            withdraw.setReceiptTime(DateUtil.date().toString());
        }
        withdraw.setId(id);
        withdraw.setExtParam(JSON.toJSONString(requestResult));
        baseMapper.updateById(withdraw);
        return RestResult.success();

    }

    @Override
    @Transactional
    public RestResult reject(AdminUser adminUser, AddCompanyWithdrawWrapper add) {
        // 获取数据
        Long id = add.getId();
        CompanyWithdraw companyWithdraw = baseMapper.selectCompanyWithdrawById(id);
        if(companyWithdraw == null)
            throw new RuntimeException("此数据不存在");


        // 更新数据
        CompanyWithdraw update = new CompanyWithdraw();
        update.setExamineState(ExamineState.reject);
        update.setExamineId(adminUser.getId());
        LambdaUpdateWrapper<CompanyWithdraw> lambdaUpdateWrapper = Wrappers.lambdaUpdate();
        lambdaUpdateWrapper.eq(CompanyWithdraw::getExamineState,ExamineState.init.getCode())
                .eq(CompanyWithdraw::getId,id);
        baseMapper.update(update, lambdaUpdateWrapper);
        return RestResult.success();

    }
}
